package com.esp.soccertutor.ui.xmlparser;

import java.util.ArrayList;

import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.esp.soccertutor.util.Log;

public class XMLParser_CommonUse {
	
	public String TAG = XMLParser_CommonUse.class.getCanonicalName();
	private String strServerXML;
	public static int STATUS_CODE;
	public static Document docCommon;
	public static int RESPONSE_CODE;
	public static String MESSAGE;
	private String Uid,Uname,UfirstName,Ucountry;
	
	public XMLParser_CommonUse(String wsURL,ArrayList<BasicNameValuePair> params){
		
		try
		{
			XMLDocument.getHttpPostXML(wsURL, params);
			strServerXML = "<response><code>1</code><message>Verified Ok</message>"
					+ "<user><userid>1</userid><username>testuser</username><firstname>Test User</firstname>"
					+ "<country>USA</country></user></response>";

//			<code>2</code>
//			<message>Invalid user name and/or password </message>
			
//			<response>
//			<code>3</code>
//			<message>Not a valid device</message>
//			</response>
		
		
			if(XMLDocument.HTTP_XML_STATUS == 200)
			{
				Log.print(TAG,strServerXML);
				docCommon = XMLDocument.XMLfromString(strServerXML.replaceAll("&","&amp;").trim());
				STATUS_CODE = 200;
				
			}
			else if(XMLDocument.HTTP_XML_STATUS == 401)
			{
				// XML not validating 
				STATUS_CODE = 401;
			}
			else if(XMLDocument.HTTP_XML_STATUS == 500)
			{
				// 	internal error occurred								
				STATUS_CODE = 500;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			STATUS_CODE = 500;
		}
	}
	
	public XMLParser_CommonUse(String wsURL){
			
		try
		{
			XMLDocument.getHttpGetXML(wsURL);
			strServerXML = "";

			if(XMLDocument.HTTP_XML_STATUS == 200)
			{
				Log.print(TAG,strServerXML);
				docCommon = XMLDocument.XMLfromString(strServerXML.replaceAll("&","&amp;").trim());
				STATUS_CODE = 200;
				
			}
			else if(XMLDocument.HTTP_XML_STATUS == 401)
			{
				// XML not validating 
				STATUS_CODE = 401;
			}
			else if(XMLDocument.HTTP_XML_STATUS == 500)
			{
				// 	internal error occurred								
				STATUS_CODE = 500;
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			STATUS_CODE = 500;
		}
	}
	
	/**
	 * THIS CALLBACK WHEN WE REACHED DOCUMENT
	 * @return
	 */
	public static Document GetDocument(){
		
		if(docCommon != null){
			return docCommon;
		}else{
			return null;
		}
	}
	
	/**
	 * Here Mode 1 For Login 0 for code and message 
	 * @param docCountry
	 * @param Mode
	 */
	public void getResponse(Document docCountry, int Mode){
		try
		{
			if(docCountry != null)
				docBuild(docCountry,Mode);
			else
				return;
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		return;
		
	}
	
	private void docBuild(Document doc,int Mode) throws Exception{
		
		if(doc != null)
		{
			Element elem$root = doc.getDocumentElement();
			NodeList nl$item = elem$root.getElementsByTagName("code");
			Element  line  = (Element)nl$item.item(0);
			RESPONSE_CODE = Integer.parseInt(XMLDocument.getCharacterDataFromElement(line, "code"));
			
			NodeList nodeMessage = elem$root.getElementsByTagName("message");
			line  = (Element)nodeMessage.item(0);
			MESSAGE = XMLDocument.getCharacterDataFromElement(line, "message");
			
			//For Login 
			if(Mode == 1){
				
				if(RESPONSE_CODE == 1)
				{
		
					NodeList nodeUid = elem$root.getElementsByTagName("userid");
					line  = (Element)nodeUid.item(0);
					Uid = XMLDocument.getCharacterDataFromElement(line, "userid");
					
					NodeList nodeUserName = elem$root.getElementsByTagName("username");
					line  = (Element)nodeUserName.item(0);
					Uname = XMLDocument.getCharacterDataFromElement(line, "username");
		
					NodeList nodeUfirstName = elem$root.getElementsByTagName("firstname");
					line  = (Element)nodeUfirstName.item(0);
					UfirstName = XMLDocument.getCharacterDataFromElement(line, "firstname");
					
					NodeList nodeUCountry = elem$root.getElementsByTagName("country");
					line  = (Element)nodeUCountry.item(0);
					Ucountry = XMLDocument.getCharacterDataFromElement(line, "country");
					
					
				}
				
			}
			
			
		}
		return;
	}
	
	public String getUserName(){
		return Uname;
	}
	public String getUserFirstName(){
		return UfirstName;
	}
	public String getUserID(){
		return Uid;
	}
	public String getUCountry(){
		return Ucountry;
	}
	
	
}
